package cn.bgs.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author Mr.JIA
 * @DATE 2021/7/16 0016 10:22
 * @PROJECT_NAME mybatis_phoenix
 * @PACKAGE_NAME cn.bgs.utils
 */
public class DateUtils {

        public static int getAge(Date birthday) {

            Calendar cal=Calendar.getInstance();
            if(cal.before(birthday)) {
                throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
            }
            int yearNow=cal.get(Calendar.YEAR);
            int monthNow=cal.get(Calendar.MONTH);
            int dayOfMonthNow=cal.get(Calendar.DAY_OF_MONTH);
            cal.setTime(birthday);
            //出生年份
            int yearBirth=cal.get(Calendar.YEAR);
            int monthBirth=cal.get(Calendar.MONTH);
            int dayOfMonthBirth=cal.get(Calendar.DAY_OF_MONTH);
            int age=yearNow-yearBirth;
            if(monthNow<=monthBirth) {
                if(monthNow==monthBirth) {
                    if(dayOfMonthNow<dayOfMonthBirth){age--;}
                }else {
                    age--;
                }
            }
            return age;
        }
    }


